home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12729 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.3 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: [Q] Experts: Multi-dim dynamic arrays
  5. Date: Tue, 02 Apr 96 11:29:11 GMT
  6. Organization: none
  7. Distribution: usa
  8. Message-ID: <828444551snz@genesis.demon.co.uk>
  9. References: <4jqc3d$lru@peabody.colorado.edu>
  10. Reply-To: fred@genesis.demon.co.uk
  11. X-NNTP-Posting-Host: genesis.demon.co.uk
  12. X-Newsreader: Demon Internet Simple News v1.27
  13. X-Mail2News-Path: genesis.demon.co.uk
  14.  
  15. In article <4jqc3d$lru@peabody.colorado.edu>
  16.            rangaswa@eddie.Colorado.EDU "qwertyuiop" writes:
  17.  
  18. >Here is a synopsis of the code I tried:  I get no compilation errors, but
  19. >during execution, I get floating point exception at the line marked XX.
  20. >(Necessary file pointers, header files etc. are included but not shown here!.)
  21. >
  22. >int **imatrix(int nrl,int nrh,int ncl,int nch);
  23. >main()
  24. >{
  25. >
  26. >int i, j; /* loop variables */
  27. >int R, C; /* number of rows, columns */
  28. >int **Table; /* table of data */
  29. >int X; /* scratch variable */
  30. >
  31. >fscanf(fileptr, "%d %d\n", &R, &C);
  32. >Table = imatrix(1,R,1,C);
  33.  
  34. You are attempting (whether successfully or not) to create an effective 2D
  35. array indexed from 1 to R, 1 to C.
  36.  
  37. >for(i = 0; i < R; i++)
  38. >   {
  39. >   for(j = 0; j < C; j++)
  40.  
  41. And now you try to use it indexed from 0 to R-1, 0 to C-1. Clearly you
  42. are going out of bounds. Allocate with:
  43.  
  44.  Table = imatrix(0,R-1,0,C-1);
  45.  
  46. >      {
  47. >        fscanf(fileptr, "%d", &X);   <------There is no need for X here, still!
  48. >        *(*(A+i)+j) = X;     XX<------floating point exception occurs here!
  49.  
  50. More commonly and clearly written as:
  51.  
  52.          A[i][j] = X;
  53.  
  54. You haven't declared A here, did you mean Table?
  55.  
  56. >      }
  57. >   fscanf(fileptr, "\n");
  58.  
  59. While it probably works here remember that "\n" matches while-space in general,
  60. it doesn't simply read until the end of the line. You don't need it before %d
  61. since that skips leading white-space.
  62.  
  63. >   }
  64. >....
  65. >}
  66. >
  67. >int **imatrix(int nrl, int nrh, int ncl, int nch) /* see numerical recipes! */
  68. >{
  69. >int i, **m;
  70. >m = (int **)malloc((unsigned)(nrh-nrl+1)*sizeof(int*));
  71. >if(!m) nerror("alloc failure, exiting!..\n");
  72. >m -= nrl;
  73. >
  74. >for(i=nrl;i<=nrh;i++)
  75. >   {
  76. >   m[i] = (int*)malloc((unsigned)(nch-ncl+1)*sizeof(int));
  77. >   if(!m[i]) nerror("alloc failure etc...\n");
  78. >   m[i] -= ncl;
  79. >   }
  80. >return m;
  81. >}
  82.  
  83. This is a classic example of illegal code and a well known problem with
  84. Numerical Recipies. The problem is that m -= nrl and m[i] -= ncl typically
  85. generate pointers pointing outside the array which results in undefined
  86. behaviour. It is OK in this case since you should be passing nrl and ncl as
  87. 0.
  88.  
  89. >----------------------------------------------------------------------------
  90. >Questions: 1. On my machine, size of int, int * are both 4 bytes. I'm using 
  91. >a gnu compiler on a Unix machine.  Why do I get a floating point exception
  92. >at the line mentioned above?
  93.  
  94. You invoked undefined behaviour - anything can happen.
  95.  
  96. >2. The imatrix function returns a pointer to an array of pointers to rows.
  97. >I ran a quick address and content check to see if my dereferencing of pointers
  98. >is faulty.  There is  a difference of 32 bytes between the address of Table[i]
  99. >and Table[i+1], how come? 
  100.  
  101. Do you mean a difference in the values?
  102.  
  103. -- 
  104. -----------------------------------------
  105. Lawrence Kirby | fred@genesis.demon.co.uk
  106. Wilts, England | 70734.126@compuserve.com
  107. -----------------------------------------
  108.